package cc.koit.blog.web.controller;

import cc.koit.blog.dao.entity.Article;
import cc.koit.blog.dao.entity.ArticleType;
import cc.koit.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gujing
 * @date 2022/2/4 2:29 下午
 * @descrption
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/types")
    public List<ArticleType> types() {
        return articleService.listTypes();
    }

    @GetMapping("/{id}")
    public Article getById(@PathVariable Long id) {
        return articleService.getById(id);
    }

    @PostMapping("/upsert")
    public Long getById(@RequestBody Article article) {
        Long id = article.getId();
        if (id == null) {
            id = articleService.create(article.getTitle(), article.getTypeId(), article.getSummary(), article.getContent(), article.getTags());
        } else {
            articleService.modify(article);
        }
        return id;
    }

}
